package vcp.application;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import java.util.Observable;
import java.util.Observer;


import javax.swing.JOptionPane;

import client.Message;
import control.SimpleClient;
import db.parking.beans.Employee;

import vcp.controllers.CheckInController;
import vcp.controllers.CheckOutController;
import vcp.controllers.ComplaintController;
import vcp.controllers.LoginController;
import vcp.controllers.OrdersGuestController;
import vcp.controllers.ParkingManagementController;
import vcp.controllers.RatesApprovalController;
import vcp.controllers.RatesController;
import vcp.controllers.RatesManagementController;
import vcp.controllers.ReportsControler;
import vcp.controllers.ResolveComplaintsController;
import vcp.controllers.SaveParkingController;
import vcp.controllers.SubscriptionController;
import vcp.controllers.SystemStateController;
import vcp.gui.components.VCPFrame;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.CheckOutForm;
import vcp.gui.forms.LoginForm;
import vcp.gui.panels.ImagePanel;
import vcp.gui.panels.TopMainScreen;
import vcp.gui.panels.customer.PnlGuestMenu;
import vcp.gui.panels.customerservice.PnlCustomerServiceMenu;
import vcp.gui.panels.networkmanager.PnlNetworkManagerMenu;
import vcp.gui.panels.parkingmanager.PnlParkingManagerMenu;
import vcp.gui.panels.parkingworker.PnlParkingWorkerMenu;
import vcp.models.CheckInModel;
import vcp.models.CheckOutModel;
import vcp.models.ComplaintsModel;

import vcp.models.LoginModel;
import vcp.models.OrdersCancelOrderModel;
import vcp.models.OrdersModel;
import vcp.models.OrdersOccasionalOrderModel;
import vcp.models.OrdersSubscriptionModel;
import vcp.models.OrdersTemporaryModel;
import vcp.models.OrdersTrackModel;
import vcp.models.ParkingManagementModel;
import vcp.models.RatesApprovalModel;
import vcp.models.RatesManagementModel;
import vcp.models.RatesModel;
import vcp.models.ReportsModel;
import vcp.models.ResolveComplaintsModel;
import vcp.models.SaveParkingModel;
import vcp.models.SubscriptionModelFull;
import vcp.models.SubscriptionModelRegular;
import vcp.models.SystemStateModel;
import vcp.views.CheckInView;
import vcp.views.CheckOutView;
import vcp.views.ComplaintsView;
import vcp.views.CustomerContactView;
import vcp.views.LoginView;
import vcp.views.OrderGuestView;
import vcp.views.ParkingManagementView;
import vcp.views.ParkingWorkerRatesView;
import vcp.views.RatesApprovalView;
import vcp.views.RatesManagementView;
import vcp.views.ReportsView;
import vcp.views.ResolveComplaintsView;
import vcp.views.SaveParkingView;
import vcp.views.SubscriptionView;
import vcp.views.SystemStateView;


/**
 * Client control class, client application initializes here 
 * Single Frame GUI client architecture
 *
 */
public class ApplicationLoader implements Observer {

	private VCPFrame mainFrame;
	private VCPPanel mainPanel;

	private OrdersModel ordersGuestModel;
	private OrderGuestView ordersGuestview;
	private OrdersGuestController ordersGuestController;
	private OrdersCancelOrderModel ordersCancelOrderModel;
	private OrdersOccasionalOrderModel ordersOccasionalOrderModel;
	private OrdersSubscriptionModel ordersSubscriptionModel;
	private OrdersTemporaryModel ordersTemporaryModel;
	private OrdersTrackModel ordersTrackModel;
	private LoginView loginView;
	private LoginModel loginModel;
	private LoginController loginController;

	private GuestView guestView;

	private SimpleClient client;

	private int _projectWidth = 900;
	private int _projectHeight = 600;
	private int _menuSidebarWidth = 200;
	private int _menuSidebarHeight = 600;
	private TopMainScreen topScreen;

	private VCPPanel pnlLeft;
	private VCPPanel pnlRight;
	private VCPPanel pnlSwitch;

	private SubscriptionView subscriptionView;
	private SubscriptionController subscriptionController;
	private SubscriptionModelFull subscriptionModel1;
	private SubscriptionModelRegular subscriptionModel2;

	private UserView userView;

	private VCPPanel userPanel;

	private SaveParkingModel saveParkingModel;

	private SaveParkingController saveParkingController;

	private ComplaintsModel complaintsModel;

	private ComplaintsView complaintsView;

	private ComplaintController complaintsController;

	private CustomerContactView customerContactView;

	private ResolveComplaintsController resolveComplaintsController;

	private ResolveComplaintsView resolveComplaintsView;

	private ResolveComplaintsModel resolveComplaintsModel;

	private CheckInModel checkInModel;

	private CheckInView checkInView;

	private CheckInController checkInController;

	private CheckOutModel checkOutModel;

	private CheckOutView checkOutView;

	private CheckOutController checkOutController;

	private SaveParkingView saveParkingView;

	private RatesController ratesController;

	private RatesModel ratesModel;

	private ParkingWorkerRatesView ratesView;

	private UserManagement userManagement;

	private ParkingManagementController parkingManagementController;

	private ParkingManagementView parkingManagementView;

	private ParkingManagementModel parkingManagementModel;

	private RatesManagementController ratesManagementController;

	private RatesManagementView ratesManagementView;

	private RatesManagementModel ratesManagementModel;

	private RatesApprovalController ratesApprovalController;

	private RatesApprovalModel ratesApprovalModel;

	private RatesApprovalView ratesApprovalView;


	private SystemStateModel systemStateModel;

	private SystemStateView systemStateView;

	private SystemStateController systemStateController;

	private ReportsControler reportsController;

	private ReportsView reportsView;

	private ReportsModel reportsModel;
	
	private String host;

	public ApplicationLoader(String host) {
		this.host = host;
		AppInit();
	}

	// here is actual login based on user properties:
	@Override
	public void update(Observable o, Object arg) {
		Employee emp = null;
		Message mess = (Message) arg;
		if (mess.getMsg().equals("Login")) {
			if (mess.isStatus()) {
				if (mess.getObj() instanceof Employee) {
					emp = (Employee) mess.getObj();
					userManagement = UserManagement.getInstance();
					userManagement.setEmployee(emp);
					JOptionPane.showMessageDialog(null , "Welcome "+ emp.getEmpName());
					initUserView();
					mess.setObj(null);
				}
			} else {
				JOptionPane.showMessageDialog(null, mess.getStatusMsg()
						.toString(), null, JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}

	
	/**
	 *  Initialize VCP
	 */
	private void AppInit() {
		initClient();
		initControllers();
		initGuestView();
		mainFrame = getMainFrame();
		

		mainFrame.addWindowListener(new CloseListener());
		mainFrame.setVisible(true);
	}

	

	/**
	 *  initialize guest GUI
	 */ 
	private void initGuestView() {
		guestView = new GuestView();
	}
    
	/**
	 * Initialize User GUI
	 */
	private void initUserView() {
		userView = new UserView();
	}

	

	
	/**
	 * Initializes client
	 */
	private void initClient() {
		if (client == null) {
			client.initClient(this.host, 12345);
			client = client.getInstance();
			open();
		}
	}

	/**
	 *  Close client
	 */
	public void close() {
		try {
			// TODO: add set host and port?
			client.closeConnection();
		} catch (Exception ex) {
			System.out.println(ex.toString());

		}
	}

	/**
	 *  opens client
	 */
	public void open() {
		try {
			client.openConnection();
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
	}

	

	/**
	 * Initialize MVC
	 */
	private void initControllers() {
		/* orders instance */
		ordersGuestModel = new OrdersModel();
		ordersCancelOrderModel = new OrdersCancelOrderModel();
		ordersOccasionalOrderModel = new OrdersOccasionalOrderModel();
		ordersSubscriptionModel = new OrdersSubscriptionModel();
		ordersTemporaryModel = new OrdersTemporaryModel();
		ordersTrackModel = new OrdersTrackModel();
		ordersGuestview = new OrderGuestView();
		ordersGuestController = new OrdersGuestController();

		/* subscriptions instance */
		subscriptionView = new SubscriptionView();
		subscriptionController = new SubscriptionController();
		subscriptionModel1 = new SubscriptionModelFull();
		subscriptionModel2 = new SubscriptionModelRegular();

		/* logins instance */
		loginView = new LoginView();
		loginModel = new LoginModel();
		loginController = new LoginController();

		/* mod orders */
		ordersOccasionalOrderModel.addObserver(ordersGuestview
				.getOrderOccasionalViewForm());
		ordersSubscriptionModel.addObserver(ordersGuestview
				.getOrderSubscriptionForm());
		ordersTemporaryModel.addObserver(ordersGuestview
				.getOrderTemporaryViewForm());
		ordersCancelOrderModel.addObserver(ordersGuestview
				.getCancelOrderViewForm());
		ordersTrackModel.addObserver(ordersGuestview.getOrderTrackViewForm());

		ordersGuestController.addModel("ordersGuestModel", ordersGuestModel);
		ordersGuestController.addModel("ordersCancelOrderModel",
				ordersCancelOrderModel);
		ordersGuestController.addModel("ordersOccasionalOrderModel",
				ordersOccasionalOrderModel);
		ordersGuestController.addModel("ordersSubscriptionModel",
				ordersSubscriptionModel);
		ordersGuestController.addModel("ordersTemporaryModel",
				ordersTemporaryModel);
		ordersGuestController.addModel("ordersCancelOrderModel",
				ordersCancelOrderModel);
		ordersGuestController.addModel("ordersTrackModel", ordersTrackModel);
		ordersGuestController.addView(ordersGuestview);
		ordersGuestview.addController(ordersGuestController);

		/* mod subscriptions */
		subscriptionModel1.addObserver(subscriptionView.getSubscriptionForm1());
		subscriptionModel2.addObserver(subscriptionView.getSubscriptionForm2());
		subscriptionController.addModel1(subscriptionModel1);
		subscriptionController.addModel2(subscriptionModel2);
		subscriptionController.addView(subscriptionView);
		subscriptionView.addController(subscriptionController);

		/* mod login */
		// loginModel.addObserver(loginView);
		loginModel.addObserver(this);
		loginController.addModel(loginModel);
		loginController.addView(loginView);
		loginView.addController(loginController);

		/* check in mod */
		checkInModel = new CheckInModel();
		checkInView = new CheckInView();
		checkInController = new CheckInController();

		checkInModel.addObserver(checkInView);
		checkInController.addModel(checkInModel);
		checkInController.addView(checkInView);
		checkInView.addController(checkInController);

		/* check out mod */
		checkOutModel = new CheckOutModel();
		checkOutView = new CheckOutView();
		checkOutController = new CheckOutController();

		checkOutModel.addObserver(checkOutView);
		checkOutController.addModel(checkOutModel);
		checkOutController.addView(checkOutView);
		checkOutView.addController(checkOutController);

		saveParkingModel = new SaveParkingModel();
		saveParkingView = new SaveParkingView();
		saveParkingController = new SaveParkingController();
		saveParkingModel.addObserver(saveParkingView);
		saveParkingController.addModel(saveParkingModel);
		saveParkingController.addView(saveParkingView);
		saveParkingView.addController(saveParkingController);

		complaintsController = new ComplaintController();
		complaintsModel = new ComplaintsModel();
		complaintsView = new ComplaintsView();

		complaintsModel.addObserver(complaintsView);
		complaintsController.addModel(complaintsModel);
		complaintsController.addView(complaintsView);
		complaintsView.addController(complaintsController);

		

		/* resolve complaints */
		resolveComplaintsController = new ResolveComplaintsController();
		resolveComplaintsView = new ResolveComplaintsView();
		resolveComplaintsModel = new ResolveComplaintsModel();

		resolveComplaintsModel.addObserver(resolveComplaintsView);
		resolveComplaintsController.addModel(resolveComplaintsModel);
		resolveComplaintsController.addView(resolveComplaintsView);

		resolveComplaintsView.addController(resolveComplaintsController);

		/* parking worker Rates */
		ratesController = new RatesController();
		ratesModel = new RatesModel();
		ratesView = new ParkingWorkerRatesView();

		ratesModel.addObserver(ratesView);
		ratesController.addModel(ratesModel);
		ratesController.addView(ratesView);
		ratesView.addController(ratesController);

		/* parking worker parking management */
		parkingManagementController = new ParkingManagementController();
		parkingManagementView = new ParkingManagementView();
		parkingManagementModel = new ParkingManagementModel();

		parkingManagementController.addModel(parkingManagementModel);
		parkingManagementController.addView(parkingManagementView);
		parkingManagementView.addController(parkingManagementController);

		/* parking manager rates management */
		/* network manager rates approval */

		ratesManagementView = new RatesManagementView();
		ratesManagementModel = new RatesManagementModel();

		ratesApprovalController = new RatesApprovalController(
				ratesManagementView);
		ratesApprovalModel = new RatesApprovalModel();
		ratesApprovalView = new RatesApprovalView();

		ratesManagementController = new RatesManagementController(
				ratesApprovalView);
		ratesManagementModel.addObserver(ratesManagementView);
		ratesManagementController.addModel(ratesManagementModel);
		ratesManagementController.addView(ratesManagementView);
		ratesManagementView.addController(ratesManagementController);

		ratesApprovalModel.addObserver(ratesApprovalView);
		ratesApprovalController.addModel(ratesApprovalModel);
		ratesApprovalController.addView(ratesApprovalView);
		ratesApprovalView.addController(ratesApprovalController);

		

		/* network manager system details */
		systemStateModel = new SystemStateModel();
		systemStateView = new SystemStateView();
		systemStateController = new SystemStateController();
		systemStateModel.addObserver(systemStateView);
		systemStateController.addModel(systemStateModel);
		systemStateController.addView(systemStateView);
		systemStateView.addController(systemStateController);
		
		/* Pworker reports */
		reportsController = new ReportsControler();
		reportsView = new ReportsView();
        reportsModel = new ReportsModel();
        reportsModel.addObserver(reportsView);
        reportsController.addModel(reportsModel);
        reportsController.addView(reportsView);
        reportsView.addController(reportsController);
        
        
        
	}

	/**
	 * Window close listener
	 *
	 */
	private class CloseListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			e.getWindow().setVisible(false);
			System.exit(0);
		}
	}


	/**
	 * Inner class user view
	 *
	 */
	private class GuestView implements ActionListener {
		private LoginForm loginForm;
		private PnlGuestMenu guestMenu;
		private CheckOutForm checkOutForm;

		public GuestView() {
			refreshPanes();
			topScreen = this.getTopScreen(null);
			loginForm = loginView.getLoginForm();
			checkOutForm = checkOutView.getForm();
			guestMenu = new PnlGuestMenu();

			guestMenu.getBtnNewOrder().addActionListener((ActionListener) this);
			guestMenu.getBtnNewOrder().setActionCommand("MenuItmNewOrder");

			guestMenu.getBtnCancelOrder().addActionListener(
					(ActionListener) this);
			guestMenu.getBtnCancelOrder()
					.setActionCommand("MenuItmCanellOrder");

			guestMenu.getBtnComplaints().addActionListener(
					(ActionListener) this);
			guestMenu.getBtnComplaints().setActionCommand("MenuItmComplaint");

			guestMenu.getBtnTrackOrder().addActionListener(
					(ActionListener) this);
			guestMenu.getBtnTrackOrder().setActionCommand("MenuItmTrackOrder");

			guestMenu.getBtnCheckIn().addActionListener((ActionListener) this);
			guestMenu.getBtnCheckIn().setActionCommand("MenuItmCheckIn");

			guestMenu.getBtnComplaints().setActionCommand("MenuItmComplaints");

			mainFrame = getMainFrame();

			mainPanel.setPreferredSize(new Dimension(_projectWidth,
					_projectHeight));

			mainPanel.setLayout(new BorderLayout());

			mainPanel.add(topScreen, BorderLayout.PAGE_START);
			pnlSwitch.setLayout(new BorderLayout());
			pnlSwitch.add(loginForm, BorderLayout.PAGE_START);
			pnlSwitch.add(checkOutForm, BorderLayout.CENTER);

			pnlLeft.setPreferredSize(new Dimension(_menuSidebarWidth,
					_menuSidebarHeight));
			Color cdblue = Color.decode("#79BEDB");

			pnlLeft.setBackground(cdblue);
			pnlLeft.add(guestMenu);

			pnlRight.setBackground(Color.white);
			pnlRight.setLayout(new BorderLayout());
			pnlRight.add(pnlSwitch, BorderLayout.CENTER);

			mainPanel.add(pnlLeft, BorderLayout.LINE_START);
			mainPanel.add(pnlRight, BorderLayout.CENTER);
			mainFrame.setContentPane(mainPanel);
			mainFrame.pack();
		}

		public TopMainScreen getTopScreen(String u) {
			if (topScreen == null) {
				topScreen = new TopMainScreen(u);
				topScreen.getBtnSubscribe().addActionListener(this);
				topScreen.getBtnSubscribe()
						.setActionCommand("TopMenuSubscribe");
				topScreen.getBtnLogin().addActionListener(this);
				topScreen.getBtnLogin().setActionCommand("TopMenuLogin");
				topScreen.getBtnCMain().addActionListener(this);
				topScreen.getBtnCMain().setActionCommand("TopMenuGuestMain");

			}
			return topScreen;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			pnlSwitch.removeAll();
			if (e.getActionCommand().equals("MenuItmNewOrder"))
				pnlSwitch.add(ordersGuestview.getWidgetPanel());
			else if (e.getActionCommand().equals("MenuItmCanellOrder"))
				pnlSwitch.add(ordersGuestview.getCancelOrderViewForm());
			else if (e.getActionCommand().equals("MenuItmTrackOrder"))
				pnlSwitch.add(ordersGuestview.getOrderTrackViewForm());
			else if (e.getActionCommand().equals("TopMenuSubscribe"))
				pnlSwitch.add(subscriptionView.getWrappedPane());
			else if (e.getActionCommand().equals("TopMenuLogin"))
				pnlSwitch.add(loginView.getLoginForm());
			else if (e.getActionCommand().equals("TopMenuGuestMain"))
				initGuestView();

			else if (e.getActionCommand().equals("MenuItmCheckIn"))
				pnlSwitch.add(checkInView.getForm());
			
			else if (e.getActionCommand().equals("MenuItmComplaints"))
				pnlSwitch.add(complaintsView.getComplaintsForm());

			pnlSwitch.revalidate();
			pnlSwitch.repaint();
			getMainFrame().pack();

		}
	}

	/**
	 * Inner class UserView
	 *
	 */
	private class UserView implements ActionListener {
		private PnlParkingManagerMenu parkingManagerMenu;
		private PnlCustomerServiceMenu customerServiceMenu;
		private PnlParkingWorkerMenu parkingWorkerMenu;
		private PnlNetworkManagerMenu directorMenu;

		public UserView() {
			refreshPanes();
			String role = UserManagement.getInstance().getEmployee().getType();
			if (!role.equals(null)) {
				if (role.equals("PWorker")) {
					loadPWorker();
				} else if (role.equals("CSWorker")) {
					loadCSWorker();
				} else if (role.equals("PManager")) {
					loadPManager();
				} else if (role.equals("Director")) {
					loadDirector();
				}

				pnlRight.add(pnlSwitch, BorderLayout.CENTER);

				userPanel = getUserPanel();
				// mainFrame = getMainFrame();

				userPanel.setPreferredSize(new Dimension(_projectWidth,
						_projectHeight));

				userPanel.setLayout(new BorderLayout());
				userPanel.add(topScreen, BorderLayout.PAGE_START);
				userPanel.add(pnlLeft, BorderLayout.LINE_START);
				userPanel.add(pnlRight, BorderLayout.CENTER);
				mainFrame.setContentPane(userPanel);
				mainFrame.pack();
			}
		}

		/**
		 * load network manager UI
		 */
		private void loadDirector() {
			directorMenu = new PnlNetworkManagerMenu();
			pnlLeft.add(directorMenu);
			directorMenu.getBtnRatesApproval().addActionListener(this);
			directorMenu.getBtnRatesApproval().setActionCommand(
					"DirectorRatesApproval");
			directorMenu.getBtnCurrentState().addActionListener(this);
			directorMenu.getBtnCurrentState().setActionCommand(
					"DirectorShowStats");
			directorMenu.getBtnSystemStats().addActionListener(this);
			directorMenu.getBtnSystemStats().setActionCommand(
					"DirectorSystemStats");
			directorMenu.getBtnQuarterReports().addActionListener(this);
			directorMenu.getBtnQuarterReports().setActionCommand("DirectorQuarterReports");
			
			directorMenu.getBtnMonthlyReports().addActionListener(this);
			directorMenu.getBtnMonthlyReports().setActionCommand("DirectorMonthlyReports");
			
			pnlSwitch.add(systemStateView.getPnlSystemDetails(),
					BorderLayout.CENTER);
			

			topScreen = this.getTopScreen("Network Manager");
		}

		/**
		 * load parking manager ui
		 */
		private void loadPManager() {
			parkingManagerMenu = new PnlParkingManagerMenu();
			pnlLeft.add(parkingManagerMenu);
			
			parkingManagerMenu.getBtnRates().addActionListener(this);
			parkingManagerMenu.getBtnRates().setActionCommand("PManagerRates");
			
			parkingManagerMenu.getBtnPrepareReports().addActionListener(this);
			parkingManagerMenu.getBtnPrepareReports().setActionCommand("PManagerReports");			
		
			pnlSwitch.add(reportsView.getPnlReportManagement(), BorderLayout.CENTER);
			pnlSwitch.repaint();

			topScreen = this.getTopScreen("Parking Manager");
		}

		/**
		 * load pworker ui
		 */
		public void loadPWorker() {
			parkingWorkerMenu = new PnlParkingWorkerMenu();

			parkingWorkerMenu.getBtnRates().addActionListener(this);
			parkingWorkerMenu.getBtnRates().setActionCommand("PWorkerRates");
			parkingWorkerMenu.getBtnParkingManagement().addActionListener(this);
			parkingWorkerMenu.getBtnParkingManagement().setActionCommand(
					"PworkerParkingManagement");
			parkingWorkerMenu.getBtnSaveParking().addActionListener(this);
			parkingWorkerMenu.getBtnSaveParking().setActionCommand(
					"PworkerSaveParking");

			pnlSwitch.add(saveParkingView.getSaveParkingPanel());
			pnlLeft.add(parkingWorkerMenu);
			pnlSwitch.revalidate();
			pnlSwitch.repaint();

			topScreen = this.getTopScreen("Parking employee");
		}

		/**
		 * load customer service ui
		 */
		public void loadCSWorker() {
			customerServiceMenu = new PnlCustomerServiceMenu();
			pnlLeft.add(customerServiceMenu);
			
			customerServiceMenu.getBtnResolveComplaints().addActionListener(
					this);
			customerServiceMenu.getBtnResolveComplaints().setActionCommand(
					"CSWorkerResolveComplaints");
			customerServiceMenu.getBtnSaveParking().addActionListener(this);
			customerServiceMenu.getBtnSaveParking().setActionCommand(
					"CSWorkerSaveParking");

			

			pnlSwitch.add(saveParkingView.getSaveParkingPanel(), BorderLayout.CENTER);
			pnlSwitch.repaint();

			topScreen = this.getTopScreen("Customer Service");

		}

		/**
		 * loads adequate top panel ui
		 * @param role
		 * @return
		 */
		public TopMainScreen getTopScreen(String role) {
			if (topScreen == null) {
				topScreen = new TopMainScreen(role);
				topScreen.getBtnLogout().addActionListener(this);
				topScreen.getBtnLogout().setActionCommand("TopMenuLogout");
				topScreen.getBtnMain().addActionListener(this);
				topScreen.getBtnMain().setActionCommand("UserMain");
				topScreen.getBtnCMain().addActionListener(this);
				topScreen.getBtnCMain().setActionCommand("TopMenuGuestMain");
			}
			return topScreen;
		}

		@Override
		public void actionPerformed(ActionEvent eve) {
			pnlSwitch.removeAll();
			if (eve.getActionCommand().equals("TopMenuLogout")) {
				UserManagement.getInstance().clearEmployeeSession();
				initGuestView();
				JOptionPane.showMessageDialog(null, "Loged out.");
			} else if (eve.getActionCommand().equals(
					"CSWorkerResolveComplaints")) {
				pnlSwitch.add(resolveComplaintsView.getComplaintsForm());
			} else if (eve.getActionCommand().equals("CSWorkerSaveParking")) {
				pnlSwitch.add(saveParkingView.getSaveParkingPanel());
			} else if (eve.getActionCommand().equals("PWorkerRates")) {
				pnlSwitch.add(ratesView.getPnlrates());
			} else if (eve.getActionCommand().equals("PworkerParkingManagement")) {
				pnlSwitch.add(parkingManagementView.getParkingManagementForm());
			} else if (eve.getActionCommand().equals("PworkerSaveParking")) {
				pnlSwitch.add(saveParkingView.getSaveParkingPanel());
			} else if (eve.getActionCommand().equals("PManagerRates")) {
				pnlSwitch.add(ratesManagementView.getPnlRatesManagement());
			}else if (eve.getActionCommand().equals("PManagerReports")) {
				pnlSwitch.add(reportsView.getPnlReportManagement());							
			} else if (eve.getActionCommand().equals("DirectorRatesApproval")) {
				pnlSwitch.add(ratesApprovalView.getPnlRatesApproval());
			} else if (eve.getActionCommand().equals("DirectorShowStats")) {
				pnlSwitch.add(reportsView.getPnlReports());
			}else if(eve.getActionCommand().equals("DirectorQuarterReports")){
				pnlSwitch.add(reportsView.getPnlQuarterReports());
			}else if(eve.getActionCommand().equals("DirectorMonthlyReports")){
				pnlSwitch.add(reportsView.getPnlMonthlyReports());
			} 
			
			else if (eve.getActionCommand().equals("DirectorSystemStats")) {
				pnlSwitch.add(systemStateView.getPnlSystemDetails());

			} else if (eve.getActionCommand().equals("TopMenuGuestMain"))
				initGuestView();
			else if (eve.getActionCommand().equals("UserMain")) {
				initUserView();
			}

			// pnlSwitch.revalidate();
			pnlSwitch.repaint();
			getMainFrame().pack();
		}
	}

	protected VCPFrame getMainFrame() {
		if (mainFrame == null) {
			mainFrame = new VCPFrame("Vertical Car Parking System 1.0");
		}
		return mainFrame;
	}

	protected VCPPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new VCPPanel();
		}
		return mainPanel;
	}

	protected VCPPanel getUserPanel() {
		if (userPanel == null) {
			userPanel = new VCPPanel();
		}
		return userPanel;
	}

	private VCPPanel getLeftPane() {
		if (pnlLeft == null) {
			pnlLeft = new VCPPanel();
		}
		return pnlLeft;
	}

	private VCPPanel getRightPane() {
		if (pnlRight == null) {
			pnlRight = new VCPPanel();
		}
		return pnlRight;
	}

	private VCPPanel getSwitchPane() {
		if (pnlSwitch == null) {
			pnlSwitch = new VCPPanel();
		}
		return pnlSwitch;
	}

	/**
	 *  Refreshes panes
	 */
	public void refreshPanes() {
		mainPanel = getMainPanel();
		mainPanel.removeAll();
		mainPanel.revalidate();
		mainPanel.repaint();
		userPanel = getMainPanel();
		userPanel.removeAll();
		userPanel.revalidate();
		userPanel.repaint();
		pnlLeft = getLeftPane();
		pnlLeft.removeAll();
		pnlLeft.revalidate();
		pnlLeft.repaint();
		pnlRight = getRightPane();
		pnlRight.removeAll();
		pnlRight.revalidate();
		pnlRight.repaint();
		pnlSwitch = getSwitchPane();
		pnlSwitch.removeAll();
		pnlSwitch.revalidate();
		pnlSwitch.repaint();
		topScreen = null;
	}

}
